package leetcode.editor.cn;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

//Java：计算二叉树的深度
public class ErChaShuDeShenDuLcof {
    public static void main(String[] args) {
        Solution solution = new ErChaShuDeShenDuLcof().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        //解法一：层序遍历，但是这个只是查最大深度。所以递归更适合
   /* public int calculateDepth(TreeNode root) {
        int result =0;
        if (root==null){
            return result;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll.left!=null){
                    queue.add(poll.left);
                }
                if (poll.right!=null){
                    queue.add(poll.right);
                }
            }
            result++;
        }
        return result;
    }*/
        public int calculateDepth(TreeNode root) {
            if (root == null) return 0;
            int leftHeight = calculateDepth(root.left);
            int rightHeight = calculateDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}